<?php
namespace Home\Common;
use \Think\Controller;

class BasicController extends Controller{
    const USER_ID = "UserID";
    const USER_NAME = "UserName";
    const SECURITY_ENCODE_STRING = "LimxIsTalent";
    const IGNORE_SECURITY_MODE = false;

    /**
     * [getImageCode 获取图片验证码]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2016-02-19T21:14:39+0800
     * @return   [type]                   [description]
     */
    public function lfnGetImageCode(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $Verify->entry();
    }

    /**
     * [CheckImgCode 验证图片验证码]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2016-02-19T21:15:43+0800
     * @param    string                   $code [description]
     * @param    string                   $id   [description]
     */
    protected function lfnCheckImgCode($code="",$id=''){
        $verify = new \Think\Verify();
        if($verify->check($code, $id)){
            return true;
        }
        else{
            return false;
        } 
    }
    
    /**
     * [ufnCheckInts 正则匹配id字符串]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2015-12-04T10:50:43+0800
     * @param    string                   $strId [id,id,id]
     * @return   [type]                          [description]
     */
    protected function ufnCheckInts($ids=""){

        $reg="/^([0-9]+,)*[0-9]+$/";
        if(preg_match($reg,$ids)){
            return true;
        }
        return false;
    }

    /**
     * 用户Token加密
     */
    protected function ufnEncodeToken($strId) {
        //若用户ID为空，直接返回空
        if (trim($strId) == "") {
            return "";
        }
        //MD5加密
        $strNewToken = md5(md5(self::SECURITY_ENCODE_STRING . date("Y-m-d")) . trim($strId));
        return $strNewToken;
    }

    /**
     * [lfnExamToken 验证用户Token有效性]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2015-12-12T09:19:42+0800
     * @param    [type]                   $strId    [uid]
     * @param    [type]                   $strToken [token]
     * @return   [type]                             [description]
     */
    protected function lfnExamToken($strId, $strToken){

        if ($strId == "" || !filter_var($strId, FILTER_VALIDATE_INT)) {
            return false;
        }

        if ($strToken == "" || strlen($strToken) != 32) {
            return false;
        }

        $strRealToken = $this->ufnEncodeToken($strId);
        if ($strRealToken != $strToken && !self::IGNORE_SECURITY_MODE) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 加密解密字符串
     * 加密:encrypt('str','E','nowamagic')
     * 解密:encrypt('被加密过的字符串','D','nowamagic')
     * $string   :需要加密解密的字符串
     * $operation:判断是加密还是解密:E:加密   D:解密
     * $key      :加密的钥匙(密匙)
     */
    public function encrypt($string,$operation,$key='') {
        $key=md5($key);
        $key_length=strlen($key);
        $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;
        $string_length=strlen($string);
        $rndkey=$box=array();
        $result='';
        for($i=0;$i<=255;$i++) {
            $rndkey[$i]=ord($key[$i%$key_length]);
            $box[$i]=$i;
        }
        for($j=$i=0;$i<256;$i++) {
            $j=($j+$box[$i]+$rndkey[$i])%256;
            $tmp=$box[$i];
            $box[$i]=$box[$j];
            $box[$j]=$tmp;
        }
        for($a=$j=$i=0;$i<$string_length;$i++) {
            $a=($a+1)%256;
            $j=($j+$box[$a])%256;
            $tmp=$box[$a];
            $box[$a]=$box[$j];
            $box[$j]=$tmp;
            $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
        }
        if($operation=='D') {
            if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)) {
                return substr($result,8);
            } else{
                return'';
            }
        } else {
            return str_replace('=','',base64_encode($result));
        }
    }

    /**
     * [lfnAjaxReturn 数据返回格式]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2015-12-15T08:54:47+0800
     * @param    string                   $strStatus [description]
     * @param    array                   $strMsg    [description]
     * @return   [type]                              [description]
     */
    protected function lfnAjaxReturn($strStatus='', $strMsg=array()){
        $arrResult = array(
                        "STATUS" => $strStatus,
                        "MESSAGE" => $strMsg,
                        "TIMESTAMP" => time()
        );
        return $arrResult;
    }

    /**
     * [lfnAjaxSuccess 数据返回格式 成功]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2015-12-21T09:38:46+0800
     * @param    array                    $strMsg [description]
     * @return   [type]                           [description]
     */
    protected function lfnAjaxSuccess($strMsg=array()){
        $arrResult = array(
                        "STATUS" => "1",
                        "MESSAGE" => $strMsg,
                        "TIMESTAMP" => time()
        );
        //return $arrResult;
        $this->ajaxReturn($arrResult);
    }

    /**
     * [lfnAjaxFailed 数据返回格式 失败]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2015-12-21T09:39:02+0800
     * @param    string                   $strMsg [description]
     * @return   [type]                           [description]
     */
    protected function lfnAjaxFailed($strMsg=""){
        $arrResult = array(
                        "STATUS" => "0",
                        "MESSAGE" => $strMsg,
                        "TIMESTAMP" => time()
        );
        //return $arrResult;
        $this->ajaxReturn($arrResult);
    }

    /**
     * [ufnGetRandomString 获取随机码]
     * @Author   Limx
     * @DateTime 2015-12-03T16:43:12+0800
     * @param    [type]                   $intLength [长度]
     * @param    string                   $strType   [类别]
     * @return   [type]                              [description]
     */
    protected function ufnGetRandomString($intLength, $strType='C')
    {
        $arrChars = array();
        if ($strType == "N") {             //获取数字随机码
            $arrChars = array( "0", "1", "2","3", "4", "5", "6", "7", "8", "9");
        } else if ($strType == "S") {    //获取字母随机码
            $arrChars = array(
                            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
                            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                            "S", "T", "U", "V", "W", "X", "Y", "Z");
        } else if ($strType == "C") {    //获取数字+字母随机码
            $arrChars = array(
                            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
                            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
                            "3", "4", "5", "6", "7", "8", "9");
        }
        $intCharsLen = count($arrChars) - 1;
        shuffle($arrChars);// 将数组打乱
        $strOutput = "";
        for ($i=0; $i<$intLength; $i++)
        {
            $strOutput .= $arrChars[mt_rand(0, $intCharsLen)]; //获得一个数组元素
        }
        return $strOutput;
    }

}